package cn.tlb;

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.MotionEvent;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

/**
 * Created by 汤林冰 on 2015/12/12 16:11.
 */
public class BaseGLActivity extends Activity {
    protected final float TOUCH_SCALE_FACTOR = 180.0f / 320;
    protected final float TRACKBALL_SCALE_FACTOR = 36.0f;
    protected GLSurfaceView mGlSurfaceView;

    private float mPreviousX;
    private float mPreviousY;
    public float mAngleX;
    public float mAngleY;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mGlSurfaceView = new GLSurfaceView(this);
    }

    @Override public boolean onTouchEvent(MotionEvent e) {
        //System.out.printf("onTouchEvent(%s)\n", e);
        float x = e.getX();
        float y = e.getY();
        switch (e.getAction()) {
            case MotionEvent.ACTION_MOVE:
                float dx = x - mPreviousX;
                float dy = y - mPreviousY;
                mAngleX += dx * TOUCH_SCALE_FACTOR;
                mAngleY += dy * TOUCH_SCALE_FACTOR;
        }
        mPreviousX = x;
        mPreviousY = y;
        return true;
    }

    protected FloatBuffer array2FloatBuffer(float fs[]){
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(fs.length * 4);
        byteBuffer.order(ByteOrder.nativeOrder());
        FloatBuffer floatBuffer = byteBuffer.asFloatBuffer();
        floatBuffer.put(fs);
        floatBuffer.position(0);
        return floatBuffer;
    }

    protected IntBuffer array2IntBuffer(int ints[]){
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(ints.length * 4);
        byteBuffer.order(ByteOrder.nativeOrder());
        IntBuffer intBuffer = byteBuffer.asIntBuffer();
        intBuffer.put(ints);
        intBuffer.position(0);
        return intBuffer;
    }

}
